package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.Sticky;
import com.draven.mapper.StickyMapper;
import com.draven.service.StickyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 便签服务实现类
 */
@Service
public class StickyServiceImpl extends ServiceImpl<StickyMapper, Sticky> implements StickyService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, String content, String color, Integer positionX, Integer positionY, Integer width, Integer height, Boolean isPinned) {
        // 创建便签
        Sticky sticky = new Sticky();
        sticky.setUserId(userId);
        sticky.setContent(content);
        sticky.setColor(color);
        sticky.setPositionX(positionX);
        sticky.setPositionY(positionY);
        sticky.setWidth(width);
        sticky.setHeight(height);
        sticky.setIsPinned(isPinned);

        save(sticky);
        return sticky.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Sticky sticky) {
        save(sticky);
        return sticky.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, Long userId, String content, String color, Integer positionX, Integer positionY, Integer width, Integer height, Boolean isPinned) {
        // 查询便签
        Sticky sticky = getDetail(id, userId);

        // 更新信息
        sticky.setContent(content);
        sticky.setColor(color);
        sticky.setPositionX(positionX);
        sticky.setPositionY(positionY);
        sticky.setWidth(width);
        sticky.setHeight(height);
        sticky.setIsPinned(isPinned);

        updateById(sticky);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Sticky sticky) {
        if (sticky.getId() == null) {
            throw new BusinessException(400, "ID不能为空");
        }

        // 查询便签并验证所有权
        Sticky existingSticky = getDetail(sticky.getId(), sticky.getUserId());

        // 更新信息
        sticky.setCreateTime(existingSticky.getCreateTime());
        updateById(sticky);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Long userId) {
        // 查询便签
        Sticky sticky = getDetail(id, userId);

        // 删除便签
        removeById(id);
    }

    @Override
    public Sticky getDetail(Long id, Long userId) {
        // 查询便签
        Sticky sticky = getById(id);
        if (sticky == null) {
            throw new BusinessException(400, "便签不存在");
        }

        // 校验所有权
        if (!sticky.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权限操作");
        }

        return sticky;
    }

    @Override
    public List<Sticky> listByUserId(Long userId) {
        // 构建查询条件
        LambdaQueryWrapper<Sticky> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sticky::getUserId, userId);
        wrapper.orderByDesc(Sticky::getUpdateTime);

        return list(wrapper);
    }

    @Override
    public Page<Sticky> page(Long userId, Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Sticky> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sticky::getUserId, userId);
        wrapper.orderByDesc(Sticky::getUpdateTime);

        // 分页查询
        return page(new Page<>(pageNum, pageSize), wrapper);
    }
} 